package com.jin.webvideo.entity;

import com.jin.webvideo.util.cloneUtil.Cloneable;
import com.jin.webvideo.util.sys.EncryptUtil;
import com.jin.webvideo.util.base.StringUtil;
import com.jin.webvideo.util.base.URLUtil;

import java.io.UnsupportedEncodingException;
import java.util.Date;
@Cloneable
public class Cache {
    public static final String STATUS_NEW = "new";
    public static final String STATUS_CACHING = "caching";
    public static final String STATUS_DEL = "del";
    public static final String STATUS_CANPLAY = "canPlay";
    public static final int TYPE_ONLY_INDEX = 1;
    public static final int TYPE_ALL = 2;

    private String parserName;
    private String url;
    private String location;
    private String fileName;
    private String status;
    private Date cacheDate;
    private Date lastVisited;
    private long fileSize;
    private Date statusDate;
    private int cacheType;
    public String getParserName() {
        return parserName;
    }

    public void setParserName(String parserName) {
        this.parserName = parserName;
    }

    public Date getStatusDate() {
        return statusDate;
    }

    public void setStatusDate(Date statusDate) {
        this.statusDate = statusDate;
    }

    public Long getFileSize() {
        return fileSize;
    }

    public void setFileSize(Long fileSize) {
        this.fileSize = fileSize;
    }

    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }

    public String getLocation() {
        return location;
    }

    public void setLocation(String location) {
        this.location = location;
    }

    public String getFileName() {
        return fileName;
    }

    public void setFileName(String fileName) {
        this.fileName = fileName;
    }

    public String getStatus() {
        return status;
    }

    public void setStatus(String status) {
        this.status = status;
    }

    public Date getCacheDate() {
        return cacheDate;
    }

    public void setCacheDate(Date cacheDate) {
        this.cacheDate = cacheDate;
    }

    public Date getLastVisited() {
        return lastVisited;
    }

    public void setLastVisited(Date lastVisited) {
        this.lastVisited = lastVisited;
    }

    public int getCacheType() {
        return cacheType;
    }

    public void setCacheType(int cacheType) {
        this.cacheType = cacheType;
    }

    public String formatKey(){
        return String.format("cache_header_%s", parserName);
    }
    public static Cache newInstance(String source, String url, String location) throws UnsupportedEncodingException {
        Cache cache = new Cache();
        Date date = new Date();
        cache.setUrl(url);
        cache.setStatus(STATUS_NEW);
        cache.setStatusDate(date);
        cache.setCacheDate(date);
        cache.setLastVisited(date);
        String fileType = "." + URLUtil.fileType(source);
        cache.setFileName(EncryptUtil.md5Hex(EncryptUtil.base64Encrypt(url)) + fileType);
        cache.setFileSize(0L);
        if (!StringUtil.isEmpty(location)){
            cache.setLocation(location);
        }
        return cache;
    }
}
